package com.dongnam.quanly.common.controller;

import java.util.List;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.dao.LoaiVatTuHangHoaDao;
import com.dongnam.quanly.common.exceptions.DatabaseException;
import com.dongnam.quanly.common.model.LoaiVatTuHangHoa;
import com.dongnam.quanly.common.model.NhomDoiTac;
import com.dongnam.quanly.common.persistence.HibernateUtil;

public class LoaiVatTuHangHoaController extends GenericController<LoaiVatTuHangHoa, String> {

	private static final Logger logger = LoggerFactory.getLogger(LoaiVatTuHangHoaController.class);

	public LoaiVatTuHangHoaController() {
		super(new LoaiVatTuHangHoaDao());
		// TODO Auto-generated constructor stub
	}

	@Override
	public LoaiVatTuHangHoaDao getDao() {
		// TODO Auto-generated method stub
		return (LoaiVatTuHangHoaDao) dao;
	}

	public boolean checkIsHasChild(String maloai) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		boolean flag = true;
		try {
			LoaiVatTuHangHoa lvthh = findById(maloai, false);
			flag = lvthh.getIsHasChild();
			if (!isTransacted) {
				session.getTransaction().commit();
			}

			return flag;
		} catch (Exception e) {
			rollbackConditionally(isTransacted, session);
			throw new DatabaseException(e);
		}
	}

	public List<LoaiVatTuHangHoa> listLoaiVatTuHangHoaExclude(List<String> listLoaiVatTuHangHoa) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<LoaiVatTuHangHoa> list = getDao().listLoaiVatTuHangHoaExclude(listLoaiVatTuHangHoa);

			if (!isTransacted) {
				session.getTransaction().commit();
			}

			return list;
		} catch (Exception e) {
			rollbackConditionally(isTransacted, session);
			throw new DatabaseException(e);
		}

	}

	public List<LoaiVatTuHangHoa> listLoaiVatTuHangHoaInclude(List<String> listLoaiVatTuHangHoa) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<LoaiVatTuHangHoa> list = getDao().listLoaiVatTuHangHoaInclude(listLoaiVatTuHangHoa);

			if (!isTransacted) {
				session.getTransaction().commit();
			}

			return list;
		} catch (Exception e) {
			rollbackConditionally(isTransacted, session);
			throw new DatabaseException(e);
		}

	}

}
